﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using HtmlAgilityPack;
using System.Net;
using System.Text.RegularExpressions;

namespace TjZheng.JiaLi.Process
{
    public static class InternetTimeHelper
    {
        public static DateTime GetInternetTime()
        {
            InvokeResult objInvokeResult = new InvokeResult { ResultCode = "" };
            HtmlWeb objHtmlWeb = new HtmlWeb();
            objHtmlWeb.OverrideEncoding = Encoding.GetEncoding("gb2312");
            HtmlDocument objHtmlDocument = objHtmlWeb.Load("http://shijian.duoshitong.com/time.php");
            string result = "";
            DateTime dateTime = DateTime.Now;
            if (objHtmlWeb.StatusCode == HttpStatusCode.OK)
            {
                HtmlNode objHtmlNode = objHtmlDocument.GetElementbyId("serverTime");
                if (objHtmlNode != null)
                {
                    result = objHtmlNode.InnerText;
                    if (result.IsNoNull())
                    {
                        result = result.Replace("年|月|日", "-", RegexOptions.None).Trim();
                        if (DateTime.TryParse(result, out dateTime))
                        {
                            objInvokeResult.ResultCode = "Success";
                        }
                    }
                }
            }


            if (objInvokeResult.ResultCode != "Success")
            {
                objHtmlWeb.OverrideEncoding = Encoding.GetEncoding("gb2312");
                objHtmlDocument = objHtmlWeb.Load("http://shijian.duoshitong.com/api/");
                if (objHtmlWeb.StatusCode == HttpStatusCode.OK)
                {
                    HtmlNode objHtmlNode = objHtmlDocument.GetElementbyId("serverTime");
                    if (objHtmlNode != null)
                    {
                        result = objHtmlNode.InnerText;
                        if (result.IsNoNull())
                        {
                            result = result.Replace("年|月|日", "-", RegexOptions.None).Trim();
                            if (DateTime.TryParse(result, out dateTime))
                            {
                                objInvokeResult.ResultCode = "Success";
                            }
                        }
                    }
                }
            }

            if (objInvokeResult.ResultCode != "Success")
            {
                objHtmlWeb.OverrideEncoding = Encoding.GetEncoding("gb2312");
                objHtmlDocument = objHtmlWeb.Load("http://biaozhunshijian.51240.com/");
                if (objHtmlWeb.StatusCode == HttpStatusCode.OK)
                {
                    HtmlNode objHtmlNode = objHtmlDocument.GetElementbyId("shijian_local");
                    if (objHtmlNode != null)
                    {
                        result = objHtmlNode.InnerText;
                        if (result.IsNoNull())
                        {
                            result = result.Replace("年|月|日", "-", RegexOptions.None).Trim();
                            if (DateTime.TryParse(result, out dateTime))
                            {
                                objInvokeResult.ResultCode = "Success";
                            }
                        }
                    }
                }
            }

            if (objInvokeResult.ResultCode != "Success")
            {
                dateTime = DateTime.Now;
            }

            return dateTime;
        }
    }
}
